<?php
/**
 * TinyCorePHP mysql基类
 * @author 	itkele@gmail.com
 * @date 	2013-02-17
 */
class db{
	public $mlink;

	public function db($dbhost, $dbuser, $dbpw, $dbname = '',$dbcharset='utf8', $pconnect=0){
		if($pconnect){
			if(!$this->mlink = @mysql_pconnect($dbhost, $dbuser, $dbpw)){
				$this->halt('无法连接到MySQL');
			}
		}else{
			if(!$this->mlink = @mysql_connect($dbhost, $dbuser, $dbpw)){
				$this->halt('无法连接到MySQL');
			}
		}
		if($this->version()>'4.1'){
			if('utf-8'==strtolower($dbcharset)){
				$dbcharset='utf8';
			}
			if($dbcharset){
				mysql_query("SET character_set_connection=$dbcharset, character_set_results=$dbcharset, character_set_client=binary", $this->mlink);
			}
			if($this->version() > '5.0.1'){
				mysql_query("SET sql_mode=''", $this->mlink);
			}
		}
		if($dbname){
			$this->select_db($dbname);
		}
	}

	public function select_db($dbname){
		return mysql_select_db($dbname, $this->mlink);
	}
	
	public function get_array($sql){
		$list = array();
		$query=$this->query($sql);
		while($row=$this->fetch_array($query)){
			$list[]=$row;
		}
		return $list;
	}
	
	public function fetch_array($query, $result_type = MYSQL_ASSOC){
		return (is_resource($query))? mysql_fetch_array($query, $result_type) :false;
	}

	public function result_first($sql){
		$query = $this->query($sql);
		return $this->result($query, 0);
	}

	public function fetch_first($sql){
		$query = $this->query($sql);
		return $this->fetch_array($query);
	}
	
	public function fetch_by_field($table,$field,$value,$select_fields='*'){
		$query=$this->query("SELECT $select_fields FROM ".DB_TABLEPRE."$table WHERE $field='$value'");
		return $this->fetch_array($query);
	}
	
	public function update_field($table,$field,$value,$where){
		return $this->query("UPDATE ".DB_TABLEPRE."$table SET $field='$value' WHERE $where");
	}
	
	public function fetch_total($table,$where='1'){
		return $this->result_first("SELECT COUNT(*) num FROM ".DB_TABLEPRE."$table WHERE $where");
	}
	
	public function query($sql, $type = ''){
		global $mquerynum;
		$func = $type == 'UNBUFFERED' && @function_exists('mysql_unbuffered_query') ? 'mysql_unbuffered_query' : 'mysql_query';
		if(!($query = $func($sql, $this->mlink)) && $type != 'SILENT'){
			$this->halt("MySQL Query Error",'TRUE',$sql);
		}
		$mquerynum++;
		return $query;
	}

	public function affected_rows(){
		return mysql_affected_rows($this->mlink);
	}

	public function error(){
		return (($this->mlink) ? mysql_error($this->mlink) : mysql_error());
	}

	public function errno(){
		return intval(($this->mlink) ? mysql_errno($this->mlink) : mysql_errno());
	}

	public function result($query, $row){
		$query = @mysql_result($query, $row);
		return $query;
	}

	public function num_rows($query){
		$query = mysql_num_rows($query);
		return $query;
	}

	public function num_fields($query){
		return mysql_num_fields($query);
	}

	public function free_result($query){
		return mysql_free_result($query);
	}

	public function insert_id(){
		return ($id = mysql_insert_id($this->mlink)) >= 0 ? $id : $this->result($this->query('SELECT last_insert_id()'), 0);
	}

	public function fetch_row($query){
		$query = mysql_fetch_row($query);
		return $query;
	}

	public function fetch_fields($query){
		return mysql_fetch_field($query);
	}

	public function version(){
		return mysql_get_server_info($this->mlink);
	}

	public function close(){
		return mysql_close($this->mlink);
	}

	public function halt($msg, $debug=true, $sql=''){
		@ini_set("date.timezone","Asia/Shanghai");
		defined(SITE_CHARSET) or define(SITE_CHARSET,'utf-8');
		if($debug){
			$output .="<html>\n<head>\n";
			$output .="<meta http-equiv=\"Content-Type\" content=\"text/html; charset=".SITE_CHARSET."\">\n";
			$output .="<title>$msg</title>\n";
			$output .="</head>\n<body><table>";
			$output .="<b>Error Info</b><table><tr><td width='100px'><b>信息</b></td><td>$msg</td></tr>\n";
			$output .="<tr><td><b>时间</b></td><td>".date("Y-m-d H:i:s")."<br /></td></tr>\n";
			$output .="<tr><td><b>文件</b></td><td> ".$_SERVER['PHP_SELF']."<br /></td></tr>\n\n";
			$output .="<tr><td><b>SQL</b></td><td> ".htmlspecialchars($sql)."<br />\n</td></tr><tr><td><b>Error</b></td><td>  ".$this->error()."</td></tr><br />\n";
			$output .="<tr><td><b>错误行号</b></td><td>  ".$this->errno()."</td></tr></table>";
			$output .="\n</body></html>";
			echo $output;
			exit();
		}
		$this->errorlog($msg,$sql);
	}
	
	public function errorlog($msg,$sql){
		$error="<?php exit;?>"."\t".time()."\t".get_client_ip()."\tMysql\t".$_SERVER['PHP_SELF']."\t".$this->errno()."\t".$this->error()."\t$sql\n";
		forcemkdir(APP_LOG."error_log");
		@$fp = fopen(APP_LOG."error_log/".date('Ymd')."_errorlog.php","a");
		@flock($fp, 2);
		@fwrite($fp,$error);
		@fclose($fp);
	}
}
?>